//
//  DXEmontionKeyBoard.swift
//  weiBoOfSwift
//
//  Created by simon on 15/12/15.
//  Copyright © 2015年 simon. All rights reserved.
//

import UIKit

class DXEmontionKeyBoard: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
       
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    // MARK: - 初始化UI
    private func setUpUI(){
//        键盘的背景颜色
    backgroundColor =  UIColor(patternImage: UIImage(named: "emoticon_keyboard_background")!)
//        设置toolBar的代理
        keyBoardToolBar.delegate = self
   
//        添加子控件
       addSubview(keyBoardToolBar)
        
        addSubview(emotionView)
        
        addSubview(pageControl)



    }
    
    // MARK: - layoutViews添加约束
    override func layoutSubviews() {
        super.layoutSubviews()
//        toolBar约束
        keyBoardToolBar.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(self)
            make.leading.equalTo(self)
            make.width.equalTo(self)
            make.height.equalTo(35)
        }
//        emotionView约束
        emotionView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self)
            make.leading.equalTo(self)
            make.trailing.equalTo(self)
            make.bottom.equalTo(keyBoardToolBar.snp_top)
        }
//        pageControl约束
        pageControl.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(keyBoardToolBar.snp_top).offset(10)
            make.centerX.equalTo(self)
        }
        
    }

    // MARK: - 懒加载
//    底部的keyBoardToolBar
    lazy var keyBoardToolBar:DXKeyBoardToolBar = {
      let keyBoardToolBar = DXKeyBoardToolBar(frame: CGRectZero)
        
        return keyBoardToolBar
    }()
//    表情区DXEmotionView
    lazy var emotionView:DXEmotionView = {
    let emotionView = DXEmotionView()
//        表情滑动的回调闭包
        emotionView.emotionViewWillScrollToNewSectionClosure = {(newSection:NSIndexPath)->() in

//            logPrint(KeyBoardToolBarType(rawValue: newSection)!)
//            当表情将要滑动到另一个section时设置toolbar将要选中的区域,
        self.keyBoardToolBar.selectedToolBarButton = KeyBoardToolBarType(rawValue: newSection.section)!
            
        self.pageControl.numberOfPages = DXEmotionViewModel.sharedEmoton.emotions![newSection.section].count
            
          self.pageControl.currentPage = newSection.item
        }
        return emotionView
    }()
//    pageController
    lazy var pageControl:UIPageControl = {
        let pageControl = UIPageControl()
        pageControl.setValue(UIImage(named: "compose_keyboard_dot_selected"), forKeyPath: "_currentPageImage")
        pageControl.setValue(UIImage(named: "compose_keyboard_dot_normal"), forKeyPath: "_pageImage")
        pageControl.numberOfPages = 1
        pageControl.hidesForSinglePage = true
        pageControl.userInteractionEnabled = false
    
    return pageControl
    }()

}

// MARK:keyToolBar的代理方法
extension DXEmontionKeyBoard:DXKeyBoardToolBarDelegate{
 
    func keyBoardToolBarClick(buttonTyp: KeyBoardToolBarType) {
        
        switch buttonTyp {
        
        case .KeyBoardToolBarLatest:
        logPrint("最近")
        case .KeyBoardToolBarDefault:
            logPrint("默认")
        case .KeyBoardToolBarEmoji:
            logPrint("Emoji")
        case .KeyBoardToolBarLxh:
            logPrint("郎小花")
        
        }
     emotionView.scrollToDestineSection = buttonTyp.rawValue
        
    self.pageControl.numberOfPages = DXEmotionViewModel.sharedEmoton.emotions![buttonTyp.rawValue].count
        
    }

}










